from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QApplication, QVBoxLayout, QLabel, QHBoxLayout, QPushButton, QLineEdit, QComboBox, \
    QTextEdit, QFileDialog, QMessageBox
from PyQt5.QtCore import Qt, QByteArray
from config import button_config,Text_config,input_config
from func import imgs
import base64


class UpdateShop(QDialog):
    def __init__(self,parent,dic):
        super().__init__()
        self.dic = dic
        self.resize(600,900)
        self.parent = parent
        self.client=self.parent.client
        self.file_path=None
        self.setWindowTitle('修改商品信息')
        self.url=self.parent.url+'/update_shop'
        main_layout = QVBoxLayout(self)
        main_layout.setSpacing(10)
        self.initUI(main_layout)
    def initUI(self,main_layout):
        label = QLabel('商品名')
        label.setStyleSheet(Text_config.text_style2)
        main_layout.addWidget(label)
        self.shop_name_input = QLineEdit()
        self.shop_name_input.setText(self.dic.get('shop_name'))
        self.shop_name_input.setStyleSheet(input_config.input_style3)
        self.shop_name_input.setPlaceholderText('请输入修改后的商品名')
        self.shop_name_input.setFixedHeight(40)
        main_layout.addWidget(self.shop_name_input)

        label = QLabel('商品价格')
        label.setStyleSheet(Text_config.text_style2)
        main_layout.addWidget(label)
        self.price_input = QLineEdit()
        self.price_input.setText(self.dic.get('price'))
        self.price_input.setStyleSheet(input_config.input_style3)
        self.price_input.setPlaceholderText('请输入修改后商品价格')
        self.price_input.setFixedHeight(40)
        main_layout.addWidget(self.price_input)

        label = QLabel('商品品牌')
        label.setStyleSheet(Text_config.text_style2)
        main_layout.addWidget(label)
        self.brand_input = QLineEdit()
        self.brand_input.setText(self.dic.get('brand'))
        self.brand_input.setStyleSheet(input_config.input_style3)
        self.brand_input.setPlaceholderText('请输入修改后的商品品牌')
        self.brand_input.setFixedHeight(40)
        main_layout.addWidget(self.brand_input)

        label = QLabel('商品种类')
        label.setStyleSheet(Text_config.text_style2)
        main_layout.addWidget(label)
        self.cobo=QComboBox()
        self.cobo.setFixedHeight(40)
        main_layout.addWidget(self.cobo)
        text_list = ['时尚女装👠', '运动男装👟', '数码产品📱', '休闲零食🍿', '儿童玩具🧸', '医疗健康💊', '办公文具💼',
                     '化妆产品💄', '材米油盐🍚', '实用工具🔨']
        for text in text_list:
            self.cobo.addItem(text)

        label = QLabel('商品描述')
        label.setStyleSheet(Text_config.text_style2)
        main_layout.addWidget(label)
        self.descrip = QTextEdit()
        self.descrip.setText(self.dic.get('description'))
        self.descrip.setStyleSheet(input_config.input_style3)
        self.descrip.setPlaceholderText('请输入修改后对该商品的描述....')
        self.descrip.setFixedHeight(160)
        main_layout.addWidget(self.descrip)

        label = QLabel('商品照片')
        label.setStyleSheet(Text_config.text_style2)
        main_layout.addWidget(label)
        self.label_img=QLabel()
        img = QPixmap()
        img.loadFromData(self.dic.get('avatar'))
        img = img.scaled(100, 100, Qt.KeepAspectRatio)
        self.label_img.setPixmap(img)
        self.label_img.setStyleSheet(input_config.input_style2)
        self.label_img.setFixedSize(100,100)

        btn_add_img=QPushButton('添加图片')
        btn_add_img.setFixedSize(80,30)
        main_layout.addWidget(self.label_img)
        main_layout.addWidget(btn_add_img)
        btn_add_img.clicked.connect(self.add_img)

        label = QLabel('数量')
        label.setStyleSheet(Text_config.text_style2)
        main_layout.addWidget(label)
        self.nums = QLineEdit()
        self.nums.setStyleSheet(input_config.input_style3)
        self.nums.setText(self.dic.get('num'))
        self.nums.setPlaceholderText('请输入修改后的库存数量')
        self.nums.setFixedHeight(40)
        main_layout.addWidget(self.nums)


        layout1=QHBoxLayout()
        layout1.setSpacing(20)
        main_layout.addLayout(layout1)
        submit_btn=QPushButton('更改')
        submit_btn.setFixedHeight(50)
        submit_btn.setStyleSheet(button_config.button_style2)
        layout1.addWidget(submit_btn)
        submit_btn.clicked.connect(self.submit)

        exit_btn=QPushButton('取消')
        exit_btn.setFixedHeight(50)
        exit_btn.setStyleSheet(button_config.button_style4)
        layout1.addWidget(exit_btn)
        exit_btn.clicked.connect(self.exit)

    def add_img(self):
        self.file_path=QFileDialog.getOpenFileName(self,'打开','./','(*.png *.jpg *.jpeg *.bmp)')[0]
        img = QPixmap(self.file_path)
        img = img.scaled(100, 100, Qt.KeepAspectRatio)
        self.label_img.setPixmap(img)

    def submit(self):
        shop_name=self.shop_name_input.text()
        price=self.price_input.text()
        brand=self.brand_input.text()
        category=self.cobo.currentText()
        description=self.descrip.toPlainText()
        file_path=self.file_path
        num=self.nums.text()
        if not shop_name or not price or not brand or not category or not description or not num:
            QMessageBox.critical(None,'错误','数据不能为空')
        if price:
            if float(price)<0:
                QMessageBox.critical(None,'错误','请输入正确的价格')
                return
        if num:
            try:
                if int(num)<0:
                    QMessageBox.critical(None,'错误','请输入正确的库存量')
                    return
            except:
                QMessageBox.critical(None,'错误','请输入正确的库存量')
        ret = QMessageBox.question(None, "提问", "确定要修改吗吗", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if ret == QMessageBox.No:
            return
        if file_path:
            with open(file_path, 'rb') as f:
                avatar = f.read()
            avatar = imgs.resize_(avatar, 128, 128)
        else:
            avatar = self.dic.get('avatar')
        param = {
            'shop_name': shop_name,
            'price': price,
            'brand': brand,
            'category': category,
            'description': description,
            'num': num,
            'avatar': avatar,
        }
        if param == self.dic:
            QMessageBox.critical(None,'错误','你还未修改任何内容')
            return
        param['seller_name']=self.parent.user_name
        param['avatar'] = base64.b64encode(avatar).decode('utf-8')
        msg=self.client.update_shop(param)
        self.close()
        self.accept()

    def exit(self):
        self.close()
        print("error")
        self.accept()

    def accept(self):
        super().accept()
